/*
 * Copyright (C) 2011 Wglxy.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.firestorm.eagletrons.features;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.firestorm.eagletrons.official.R;
import com.firestorm.eagletrons.dashboard.DashboardActivity;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

public class WebActivity extends DashboardActivity {
	ProgressBar pbar1;
	ProgressBar pbar2;
	WebView webview;
	GoogleAnalyticsTracker tracker;
	String STRING_URL;
	String STRING_TITLE;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feature_web);
		STRING_TITLE = getIntent().getStringExtra("title");
		this.setTitle(STRING_TITLE);
		setTitleFromActivityLabel(R.id.title_text);
		STRING_URL = getIntent().getStringExtra("url");

		pbar1 = (ProgressBar) findViewById(R.id.web_pbar1);
		pbar2 = (ProgressBar) findViewById(R.id.web_pbar2);
		webview = (WebView) findViewById(R.id.web_webview);

		tracker = GoogleAnalyticsTracker.getInstance();
		tracker.startNewSession("UA-25025598-9", this);
		tracker.trackPageView("/" + STRING_TITLE);

		pbar1.setVisibility(View.VISIBLE);
		pbar2.setVisibility(View.VISIBLE);
		webview.getSettings().setJavaScriptEnabled(true);
		final Activity activity = this;
		webview.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				if (View.GONE == pbar1.getVisibility()) {
					pbar1.setVisibility(View.VISIBLE);
				}
				if (View.GONE == pbar2.getVisibility()) {
					pbar2.setVisibility(View.VISIBLE);
				}
				pbar2.setProgress(progress);
				if (progress == 100) {
					pbar1.setVisibility(View.GONE);
					pbar2.setVisibility(View.GONE);
				}
			}
		});
		webview.setWebViewClient(new WebViewClient() {
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				Toast.makeText(
						activity,
						"Could not load page. Please check your internet connection.",
						Toast.LENGTH_SHORT).show();
			}
		});
		webview.getSettings().setUserAgentString("android_eagletrons");
		webview.setHorizontalScrollBarEnabled(false);
		webview.setVerticalScrollBarEnabled(false);
		webview.loadUrl(STRING_URL);
	}

	@Override
	public void onConfigurationChanged(final Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onPause() {
		super.onPause();
		tracker.dispatch();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		tracker.stopSession();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_webview, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_home:
			webview.loadUrl(STRING_URL);
			return true;
		case R.id.menu_reload:
			webview.reload();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (webview.canGoBack()) {
				webview.goBack();
			} else {
				finish();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
} // end class
